package com.juyu.miliao.api.service;

import com.juyu.miliao.api.dao.mysql.BillDetailIntegralDao;
import com.juyu.miliao.api.util.PageUtil;
import com.juyu.miliao.common.constant.IntegralRewardsAndPunishmentsType;
import com.juyu.miliao.common.domain.BillDetailIntegral;
import com.juyu.miliao.common.domain.vo.BillDetailIntegralVO;
import com.juyu.miliao.common.domain.vo.IntegralRewardsAndPunishmentsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xuanqicheng
 */
@Service
public class BillDetailIntegralService {

    @Autowired
    private BillDetailIntegralDao billDetailIntegralDao;

    public List<IntegralRewardsAndPunishmentsVO> getListByType(int type) {
        List<IntegralRewardsAndPunishmentsVO> list = new ArrayList<>();
        for (IntegralRewardsAndPunishmentsType e : IntegralRewardsAndPunishmentsType.values()) {
            IntegralRewardsAndPunishmentsVO obj = new IntegralRewardsAndPunishmentsVO();
            if(e.getType() == type && e.getIsShow()==1){
                obj.setTitle(e.getTitle());
                obj.setDesc(e.getDesc());
                obj.setIcon(e.getIcon());
                if(e.getIntegralValue()>=0){
                    obj.setIntegralValue("+"+e.getIntegralValue()+"积分");
                }else {
                    obj.setIntegralValue(e.getIntegralValue()+"积分");
                }
                list.add(obj);
            }

        }
        return list;
    }

    public Map<String,IntegralRewardsAndPunishmentsType> getIntegralRewardsAndPunishmentsTypeToMap(){
        Map<String,IntegralRewardsAndPunishmentsType> map = new HashMap<>();
        for (IntegralRewardsAndPunishmentsType e : IntegralRewardsAndPunishmentsType.values()) {
            map.put(Integer.toString(e.getCode()),e);
        }
        return map;
    }

    public List<BillDetailIntegralVO> getBillDetailIntegralByUserId(long userId,
                                                                    Integer page, Integer pageSize) {
        int startNum = PageUtil.getOffset(page, pageSize);
        List<BillDetailIntegral> list = billDetailIntegralDao.getBillDetailIntegralByUserId(userId, startNum, pageSize);
        List<BillDetailIntegralVO> resultList = new ArrayList<>();
        Map<String, IntegralRewardsAndPunishmentsType> map = getIntegralRewardsAndPunishmentsTypeToMap();
        for(BillDetailIntegral obj : list){
            BillDetailIntegralVO vo = new BillDetailIntegralVO();
            BeanUtils.copyProperties(obj,vo);
            IntegralRewardsAndPunishmentsType type = map.get(Integer.toString(obj.getIntegralRewardsPunishmentsType()));
            vo.setTitle(type.getTitle());
            StringBuilder sb = new StringBuilder();
            if(obj.getIntegralValue()>=0){
                vo.setType(1);
                sb.append("+").append(obj.getIntegralValue()).append("积分");

            }else{
                vo.setType(2);
                sb.append(obj.getIntegralValue()).append("积分");
            }

            vo.setIntegral(sb.toString());
            resultList.add(vo);
        }
        return resultList;
    }
}
